/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: homeview.cpp 401 2009-05-10 09:58:00Z jpnurmi $
*/

#include "homeview.h"
#include <QDesktopServices>
#include <QVBoxLayout>
#include <QTabBar>
#include <QLabel>
#include <QDebug>
#include <QFile>
#include <QUrl>

HomeView::HomeView(QWidget* parent) : QTabWidget(parent)
{
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout(widget);
    layout->setMargin(0);

    QLabel* label = new QLabel(widget);
    connect(label, SIGNAL(linkActivated(QString)), this, SLOT(linkActivated(QString)));

    QFile cssFile(":/resources/style.css");
    cssFile.open(QIODevice::ReadOnly);
    label->setStyleSheet(cssFile.readAll());

    QFile htmlFile(":/resources/index.html");
    htmlFile.open(QIODevice::ReadOnly);
    label->setText(htmlFile.readAll());

    layout->addWidget(label);
    addTab(widget, QString());
    tabBar()->hide();
}

void HomeView::linkActivated(const QString& link)
{
    QUrl url(link);
    if (url.scheme() != "commini")
    {
        QDesktopServices::openUrl(url);
        return;
    }
    
    if (url.path() == "connect")
        emit connectTo(QString(), -1, QString(), QString());
    else if (url.path() == "settings")
        emit openSettings();
    else if (url.path() == "about")
        emit aboutCommini();
    else if (url.path() == "aboutqt")
        emit aboutQt();
    else
        qWarning() << "HomeView::linkActivated(): unknown url" << url;
}
